package com.example.yuejian.yuejian.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.fragment.ClubClassFragment;
import com.example.yuejian.yuejian.fragment.ClubIntroFragment;
import com.example.yuejian.yuejian.fragment.ClubVideoFragment;

import java.util.ArrayList;
import java.util.List;

import static com.example.yuejian.yuejian.view.Carousel.setCarousel;

public class ClubDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabClubIntro;
    private LinearLayout mTabClubClass;
    private LinearLayout mTabClubVideo;
    //private LinearLayout mTabAll;

    private TextView tvClubIntro;
    private TextView tvClubClass;
    private TextView tvClubVideo;

    private Fragment mFmClubIntro;
    private Fragment mFmClubClass;
    private Fragment mFmClubVideo;

    private ImageView btn_back;

    private Context context;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private String[] image = {"company1.jpg","company2.jpg","company3.jpg","company4.jpg"};
    private List<String> images =new ArrayList<>();
    // private List<String> imgUrl =new ArrayList<>();
    // String imgUrl;
    String imgUrl="http://123.207.174.155/zqb/Public/goodsImg/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        initView();
        initEvents();
        setSelect(0);
        back();
    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_club_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private void initView() {
        mTabClubIntro=(LinearLayout)findViewById(R.id.id_tab_club_intro);
        mTabClubClass=(LinearLayout)findViewById(R.id.id_tab_club_class);
        mTabClubVideo=(LinearLayout)findViewById(R.id.id_tab_club_video);

       //mTabAll=(LinearLayout)findViewById(R.id.id_tab_home_top_all);

        tvClubIntro=(TextView)findViewById(R.id.tv_tab_club_intro);
        tvClubClass=(TextView)findViewById(R.id.tv_tab_club_class);
        tvClubVideo=(TextView)findViewById(R.id.tv_tab_club_video);



        for (int i = 0; i < 4; i++) {
            images.add(imgUrl + image[i]);
        }
        //context = getActivity();
        li = (LinearLayout) findViewById(R.id.play);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        convenient = (ConvenientBanner)findViewById(R.id.convenientBanner);
        Display d = getWindowManager().getDefaultDisplay();
        //*获取布局参数*//*
        i = li.getLayoutParams();
        i.height = d.getHeight() / 3;
        i.width = d.getWidth();
        setCarousel(context, convenient, images, i.width, i.height);
       /* CarouselModel carouselModel=new  CarouselModel();
        Call<List<Carousel>> call1=carouselModel.Carousel();
        call1.enqueue(new Callback<List<Carousel>>() {
            @Override
            public void onResponse(Call<List<Carousel>> call1, retrofit2.Response<List<Carousel>> response)
            {
                List<Carousel> res=response.body();
                for (int i=0;i<res.size();i++)
                {
                    images.add(res.get(i).getPhoto());
                }
                context=getActivity();
                li=(LinearLayout)view.findViewById(R.id.play);
                // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
                convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
                Display d=getActivity().getWindowManager().getDefaultDisplay();
               *//*获取布局参数*//*
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);

            }
            @Override
            public void onFailure(Call<List<Carousel>> call, Throwable t) {

            }
        });

    }*/
    }
    private void initEvents(){
        mTabClubIntro.setOnClickListener(this);
        mTabClubClass.setOnClickListener(this);
        mTabClubVideo.setOnClickListener(this);
    }

    private void resetTextColors() {

        tvClubIntro.setTextColor(ContextCompat.getColor(getApplication(), R.color.gray));
        tvClubClass.setTextColor(ContextCompat.getColor(getApplication(), R.color.gray));
        tvClubVideo.setTextColor(ContextCompat.getColor(getApplication(), R.color.gray));
    }
    private  void resetBgColors(){
        mTabClubIntro.setBackgroundResource(R.drawable.shape_home_top);
        mTabClubClass.setBackgroundResource(R.drawable.shape_home_top);
        mTabClubVideo.setBackgroundResource(R.drawable.shape_home_top);
        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_club_intro:
                setSelect(0);
                break;
            case R.id.id_tab_club_class:
                setSelect(1);
                break;
            case R.id.id_tab_club_video:
                setSelect(2);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm =getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmClubIntro == null)
                {
                    mFmClubIntro = new ClubIntroFragment();
                    transaction.add(R.id.id_content_club_detail, mFmClubIntro);
                } else
                {
                    transaction.show(mFmClubIntro);
                }
                tvClubIntro.setTextColor(ContextCompat.getColor(getApplication(), R.color.white));
                mTabClubIntro.setBackgroundColor(ContextCompat.getColor(getApplication(), R.color.gray));

                break;
            case 1:
                if (mFmClubClass == null)
                {
                    mFmClubClass = new ClubClassFragment();
                    transaction.add(R.id.id_content_club_detail, mFmClubClass);
                } else
                {
                    transaction.show(mFmClubClass);

                }

                tvClubClass.setTextColor(ContextCompat.getColor(getApplication(), R.color.white));
                mTabClubClass.setBackgroundColor(ContextCompat.getColor(getApplication(), R.color.gray));

                break;
            case 2:
                if (mFmClubVideo == null)
                {
                    mFmClubVideo = new ClubVideoFragment();
                    transaction.add(R.id.id_content_club_detail, mFmClubVideo);
                } else
                {
                    transaction.show(mFmClubVideo);

                }

                tvClubVideo.setTextColor(ContextCompat.getColor(getApplication(), R.color.white));
                mTabClubVideo.setBackgroundColor(ContextCompat.getColor(getApplication(), R.color.gray));

                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmClubIntro != null)
        {
            transaction.hide(mFmClubIntro);
        }
        if (mFmClubClass != null)
        {
            transaction.hide(mFmClubClass);
        }
        if (mFmClubVideo != null)
        {
            transaction.hide(mFmClubVideo);
        }

    }
}
